package com.apestech.framework.db;

import com.apestech.formwork.xml.core.NodeVar;
import com.apestech.framework.util.DateUtil;
import com.apestech.framework.util.FormatUtil;

import java.sql.Clob;
import java.util.Date;

/**
 * @author hup
 * @create 2018-11-30-11:50
 */
public class FieldFormatInfo extends FormatUtil {

    private Format.FormatField field;
    private String type;
    private String math;
    private Object formatValue;
    private Double numValue;
    private Date dateValue;
    private Object value;
    private boolean xls;
    private String pattern;
    private boolean bmath=false;

    public FieldFormatInfo(Format.FormatField field, Object value, boolean xls, boolean bmath) throws Exception {
        if(field==null) return;
        this.field=field;
        this.value=value;
        this.xls=xls;
        this.bmath=bmath;
        if(this.xls) this.bmath=false;
        init();
    }

    private void init() throws Exception {
        if(field!=null){
            type=field.getType();
            math=field.getMath();
            if(math!=null && type==null){
                type="";
            }
        }
        if("number".equals(type) || "amount".equals(type) || "percent".equals(type) || "permillage".equals(type)){
            numValue=castToDouble(value);
            switch (type){
                case "number":{
                    setNumber();
                    break;
                }
                case "amount":{
                    setAmount();
                    break;
                }
                case "percent":{
                    setPercent();
                    break;
                }
                case "permillage":{
                    setPermillage();
                    break;
                }
            }
        }
        if(value!=null){
            String sclass=value.getClass().getName().toUpperCase();
            if(sclass.indexOf("TIMESTAMP")>=0 || sclass.indexOf("DATE")>=0){
                if(sclass.indexOf("DATE")>=0) dateValue= (Date) value;
                else if(value instanceof oracle.sql.TIMESTAMP){
                    dateValue= new java.util.Date(((oracle.sql.TIMESTAMP)value).timestampValue().getTime());
                }
                if("timeStamp".equals(type) || field.equals(NodeVar.getDefaultTimestampField())){
                    if(xls) formatValue=dateValue;
                    else formatValue=DateUtil.getTimeStampStr(dateValue);
                } else formatValue=dateValue;
            }
            if(sclass.indexOf("CLOB")>=0){
                formatValue=castClobToString((Clob) value);
            }
            if(formatValue==null) formatValue=value;
        }
    }

    private void setNumber() throws Exception {
        if(xls) pattern=getNumPattern(field.getScale(), field.getLocale());
        if(bmath){
            switch (math){
                case "ceil":{
                    formatValue=formatCurrencyCeil(numValue, field.getScale(), field.getLocale());
                    break;
                }
                case "floor":{
                    formatValue=formatCurrencyFloor(numValue, field.getScale(), field.getLocale());
                    break;
                }
                case "round":{
                    formatValue=formatCurrencyRound(numValue, field.getScale(), field.getLocale());
                    break;
                }
            }
        } else formatValue=numValue;
    }

    private void setAmount() throws Exception {
        if(xls){
            pattern=getCurrencySymbol(getCurrencyShorthand(field.getLocale()))+getCurrencyPattern(field.getScale(), field.getLocale());
            formatValue=numValue;
            return;
        }
        if(bmath){
            switch (math){
                case "ceil":{
                    formatValue=formatCurrencySymbolCeil(numValue, field.getScale(), field.getLocale());
                    break;
                }
                case "floor":{
                    formatValue=formatCurrencySymbolFloor(numValue, field.getScale(), field.getLocale());
                    break;
                }
                case "round":{
                    formatValue=formatCurrencySymbolRound(numValue, field.getScale(), field.getLocale());
                    break;
                }
            }
        } else formatValue=formatCurrencySymbol(numValue, field.getLocale());
    }

    private void setPercent() throws Exception {
        if(xls){
            pattern=getPercentPattern(field.getScale(), field.getLocale());
            formatValue=numValue;
            return;
        }
        if(bmath){
            switch (math){
                case "ceil":{
                    formatValue=formatNumberPercentCeil(numValue, field.getScale());
                    break;
                }
                case "floor":{
                    formatValue=formatNumberPercentFloor(numValue, field.getScale());
                    break;
                }
                case "round":{
                    formatValue=formatNumberPercentRound(numValue, field.getScale());
                    break;
                }
            }
        }else formatValue=formatNumberPercent(numValue, field.getScale());
    }

    private void setPermillage() throws Exception {
        if(xls){
            pattern=getPermillagePattern(field.getScale(), field.getLocale());
            formatValue=numValue;
            return;
        }
        if(bmath){
            switch (math){
                case "ceil":{
                    formatValue=formatNumberPermillageCeil(numValue, field.getScale());
                    break;
                }
                case "floor":{
                    formatValue=formatNumberPermillageFloor(numValue, field.getScale());
                    break;
                }
                case "round":{
                    formatValue=formatNumberPermillageRound(numValue, field.getScale());
                    break;
                }
            }
        } else formatValue=formatNumberPermillage(numValue, field.getScale());
    }

    public Object getFormatValue() {
        return formatValue;
    }

    public String getPattern() {
        return pattern;
    }
}


